from sys import exc_info
from muMod import *
from win32.win32gui import FindWindow, SetWindowPos
wins = []

title = "永恒之巅"
hwnd = FindWindow(None, title)
#hwnd = 721750
SetWindowPos(hwnd, -2, 736, 0, 864, 530, 1) #窗口置顶 736, 332；2016, 456
sleep(1)

window={"hWnd":hwnd, "title":title, "map":"vip", "mode":"vip", "bound":[],
	"coords":[], "last_coord":[0.6, 0.51],
	"black_coords":[],
	"black_len": 0, "last_time":time(), "tsleep":[], "line":1, "turn":0, "hour":24}
window["black_len"] = len(window["black_coords"])

def vip_Play(window, monitor = 0, top = False, fen = False):
	global info
	try:
		n = 0 #循环外初始化
		info = ''
		while n < 20:
			hWnd = window["hWnd"]
			diagClose(hWnd, monitor, window["map"], window["line"], top) # 检测界面异常
			if len(window["black_coords"]) > window["black_len"] and (time() - window["last_time"] > 120):
				del window["black_coords"][window["black_len"]] # 移出坐标黑名单
			act_stat = handAct(hWnd, monitor, top)
			if not act_stat[0]:
				map_result = ''
				while map_result != window["map"] :
					close_all(hWnd, monitor)
					bkg_click(hWnd, (770,110)) # 打开地图
					bkg_click(hWnd, (759, 390), True, -20) # 放大
					sleep(0.5)
					image = loc_capture(hWnd, monitor, top)
					map_result = map_check(image[89:107, 250:350]) # 地图
					if map_result != window["map"]:
						bkg_click(hWnd, (102,97)) # 关闭地图
						map_switch(hWnd, window["map"]); sleep(2)
						line_switch(hWnd, window["line"]) #切回原来线路
						sleep(2)
						break
				else:
					sleep(2.5)
					player_des = getPlayer(image)
					if not player_des: # 未获取玩家坐标
						player_des = {"result":window["last_coord"]} # 用上次坐标代替玩家坐标
					window["coords"] = bossls(player_des["result"], image, window["black_coords"])
					if type(window["coords"]) is bool:
						bkg_click(hWnd, (102,97)) # 关闭地图
						continue
					bkg_click(hWnd, window["coords"][0][0]) # 点击最近目标
					ts = int(2 * np.sqrt(window["coords"][0][1]))-1 if window["coords"][0][1] >10 else int(2 * np.sqrt(window["coords"][0][1]))
					sleep(ts) # 2 * 距离平方根
					bkg_click(hWnd, (102,97)) # 关闭地图
					bkg_click(hWnd, coords_spec["hand"]); sleep(2)  # 点击手动
					window["last_coord"] = list(window["coords"][0][0]) # 更新上次选择坐标
					sleep(2)
					act_stat = handAct(hWnd, monitor, top)
					if not act_stat[0]:	# 没到呢, 继续走
						bkg_click(hWnd, coords_spec["map"]) # 打开地图
						bkg_click(hWnd, (759, 390), True, -20) # 放大
						sleep(0.5)
						bkg_click(hWnd, window["coords"][0][0], int(2 * np.sqrt(window["coords"][0][1]))) # 继续之前目标
						sleep(5)
						bkg_click(hWnd, (102,97)) # 关闭地图
						bkg_click(hWnd, coords_spec["hand"], 2)  # 点击手动
			elif act_stat[0] and not act_stat[2]:
				bkg_click(hWnd, coords_spec["hand"])  # 点击手动
			elif act_stat[0] and not act_stat[1]: # 抢归属
				 window["black_coords"] += window["coords"][0][0]
				 window["last_time"] = time()
				 continue
			n +=1
#		bkg_click(hWnd, (510,500)); sleep(1) #续天使
		bkg_click(hWnd, (350,320))
		# 回收
		bkg_click(hWnd, coords_spec["pack"]); sleep(3)
		bkg_click(hWnd, (815,485)); sleep(2) # 整理
		bkg_click(hWnd, coords_spec["recycle1"]); sleep(3)
		bkg_click(hWnd, coords_spec["recycle2"]); sleep(3)
		#close_all(rect, monitor, hWnd)
		bkg_click(hWnd, (609,62))
		#sleep(tsleep)
		if fen: # 分解
			bkg_click(hWnd, (850,168)); sleep(1)
			bkg_click(hWnd, (850,316)); sleep(2)	#锻造
			bkg_click(hWnd, (850,300)); sleep(1)	#分解
			for fx in range(350,530,55):
				bkg_click(hWnd, (fx, 190))
				sleep(0.5)
			bkg_click(hWnd, (700,490)); sleep(1)	#分解
			bkg_click(hWnd, (565,62))
			sleep(3)
	except KeyboardInterrupt:
		print(strftime("%m-%d %H:%M", localtime()), "sleep 30s.")
		sleep(30)
		print('...') # going on
		pass
	except:
		print("Unexpected error:", exc_info()[0:2])
		pass

while True:
	vip_Play(window, 0, True, False)
	sleep(5)
